package com.mock.ems.controller.cooperation;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.mock.base.util.CollectionUtil;
import com.mock.base.util.DateUtil;
import com.mock.base.util.StringUtil;
import com.mock.ems.common.JsonResult;

import java.util.*;

/**
 * Created by MOCK on 2017/8/18.
 */
public class UserAuthController extends Controller {

    public void index() {
        renderJsp("/page/cooperation/userauth/user_auth_list.jsp");
    }

    public void pagination() {
        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");

        StringBuilder exceptSelectSQL = new StringBuilder(" from user_auth_record where 1 = 1 ");
        List<Object> params = new ArrayList<Object>();

        String nickName = getPara("nick_name");
        if (StringUtil.isNotEmpty(nickName)) {
            List<Record> userList = Db.find("select id from user where nick_name like ?",'%' + nickName + '%');
            if (CollectionUtil.isNotEmpty(userList)) {
                exceptSelectSQL.append(" and user_id in ( ");
                userList.forEach(item0 -> {
                    exceptSelectSQL.append("?,");
                    params.add(item0.getLong("id"));
                });
                exceptSelectSQL.deleteCharAt(exceptSelectSQL.length() - 1);
                exceptSelectSQL.append(") ");
            }
        }

        Date begin_time = DateUtil.parseToDate(getPara("begin_time"),DateUtil.FULL_PATTERN);
        Date end_time = DateUtil.parseToDate(getPara("end_time"),DateUtil.FULL_PATTERN);

        if (null != begin_time && end_time == null) {
            exceptSelectSQL.append(" and apply_time >= ? ");
            params.add(begin_time);
        }

        if (null == begin_time && end_time != null) {
            exceptSelectSQL.append(" and apply_time <= ? ");
            params.add(end_time);
        }

        if (null != begin_time && null != end_time) {
            exceptSelectSQL.append(" and apply_time >= ? and apply_time <= ? ");
            params.add(begin_time);
            params.add(end_time);
        }


        exceptSelectSQL.append(" order by apply_time desc ");
        Page<Record> recordList = null;
        if (params.size() == 0) {
            recordList = Db.paginate(pageNumber, pageSize, "select * ", exceptSelectSQL.toString());
        }else {
            recordList = Db.paginate(pageNumber, pageSize, "select * ", exceptSelectSQL.toString(),params.toArray());
        }

        if (CollectionUtil.isNotEmpty(recordList.getList())) {
            recordList.getList().forEach(item -> {
                Long user_id = item.getLong("user_id");
                Record userRecord = Db.findById("user",user_id);
                if (null != userRecord) {
                    item.set("nick_name",userRecord.get("nick_name"));
                }
            });
        }

        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put("total", recordList.getTotalRow());
        returnMap.put("rows", recordList.getList());
        renderJson(returnMap);
    }

    public void audit() {

        String id = getPara("id");
        Record record = Db.findById("user_auth_record",id);
        if (null != record) {
            Long user_id = record.getLong("user_id");
            Record user = Db.findById("user",user_id);
            if (null != user) {
                user.set("status_str",user.getInt("status") == 1 ? "正常" : "冻结");
                record.set("user",user.getColumns());
            }
            String id_card_images = record.get("id_card_images");
            if (StringUtil.isNotEmpty(id_card_images)) {
                record.set("front_image",id_card_images.split(",")[0]);
                record.set("backend_image",id_card_images.split(",")[1]);
            }
        }
        setAttr("record",record.getColumns());
        renderJsp("/page/cooperation/userauth/user_audit.jsp");
    }

    @Before(Tx.class)
    public void auditUpdate() {

        String id = getPara("id");
        Integer status = getParaToInt("status");
        Record record = Db.findById("user_auth_record",id);

        record.set("apply_result",status);
        record.set("handle_time",new Date());

        if (status == 1) {
            Record user = Db.findById("user",record.getLong("user_id"));
            if (null != user) {
                user.set("authed",1);
                user.set("update_time",new Date());
                if (!Db.update("user",user)) {
                    throw new RuntimeException("update user error");
                }
            }
        }
        if (!Db.update("user_auth_record",record)) {
            throw new RuntimeException("update user_auth_record fail");
        }
        renderJson(new JsonResult().status("000").show("操作成功"));
    }
}
